#include "pageaccumlations.h"
#include "ui_pageaccumlations.h"

PageAccumlations::PageAccumlations(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageAccumlations)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);

    m_pKeyBoard = new SlimNumKeyBoard(this);
    m_pKeyBoard->move(150,720);
    m_pKeyBoard->setFocusPolicy(Qt::NoFocus);
    connect(m_pKeyBoard,SIGNAL(setNumber(int)),this,SLOT(input_slot(int)));
    connect(m_pKeyBoard,SIGNAL(backspace()),this,SLOT(backspace_slot()));
    connect(m_pKeyBoard,SIGNAL(cancel()),this,SLOT(cancel_slot()));
    m_pKeyBoard->hide();

    inputlist.append(ui->lineEdit_TotalDistance);
    inputlist.append(ui->lineEdit_TotalRunTime);
    inputlist.append(ui->lineEdit_TotalRunTime_Air_1);
    inputlist.append(ui->lineEdit_TotalRunTime_Air_6);
    inputlist.append(ui->lineEdit_ProEnergyConsume);
    inputlist.append(ui->lineEdit_RegEnergyConsume);
    inputlist.append(ui->lineEdit_BrakeResistEnergyConsume);
    inputlist.append(ui->lineEdit_AuxEnergyConsume);
    for(int i=0; i<inputlist.size(); ++i)
        inputlist.at(i)->installEventFilter(this);

    setPageActive(false);
}

PageAccumlations::~PageAccumlations()
{
    delete ui;
}

void PageAccumlations::setPageActive(bool xActive)
{
    ui->Btn_TotalDistanceSet->setEnabled(xActive);
    ui->Btn_TotalRunTimeSet->setEnabled(xActive);
    ui->Btn_CompRunTImeSet1->setEnabled(xActive);
    ui->Btn_CompRunTImeSet4->setEnabled(xActive);
    ui->Btn_ProEnergySet->setEnabled(xActive);
    ui->Btn_AuxEnergySet->setEnabled(xActive);
}

void PageAccumlations::on_Btn_Return_clicked()
{
    emit returnToMaintMenu();
}

bool PageAccumlations::eventFilter(QObject *obj, QEvent *event)
{
    for(int i=0; i<inputlist.size(); ++i)
    {
        if(event->type() == QEvent::MouseButtonPress)
        {
            if(obj == inputlist.at(i))
            {
                inputlist.at(i)->setStyleSheet("background-color: rgb(100,255,255); border: 1px solid white;font:20px 'Arial';");
                m_pKeyBoard->show();
                m_pFocusLinedit = inputlist.at(i);
            }
            else
                inputlist.at(i)->setStyleSheet("background-color: white; border: 1px solid white;font:20px 'Arial';");
        }
    }
    return QWidget::eventFilter(obj, event);
}

void PageAccumlations::input_slot(int number)
{
    QKeyEvent numKey(QEvent::KeyPress, (Qt::Key)(Qt::Key_0+number), Qt::NoModifier, QString::number(number));
    QApplication::sendEvent(m_pFocusLinedit, &numKey);
}

void PageAccumlations::backspace_slot()
{
    QKeyEvent backspaceKey(QEvent::KeyPress,Qt::Key_Backspace,Qt::NoModifier);
    QApplication::sendEvent(m_pFocusLinedit,&backspaceKey);
}

void PageAccumlations::cancel_slot()
{
    m_pKeyBoard->hide();
}

void PageAccumlations::setDayDistance(uint val)
{
    ui->label_DayDistance->setText(QString("%1 km").arg(val));
}

void PageAccumlations::setTotalRunTime(uint val)
{
    ui->label_TotalRunTime->setText(QString("%1时%2分").arg(val/60).arg(val%60));
}

void PageAccumlations::setTotalDistance(uint val)
{
    ui->label_TotalDistance->setText(QString("%1 km").arg(val));
}

void PageAccumlations::setTotalACRunTime(uint8_t index, uint32_t val)
{
    if(index == 0)
        ui->label_TotalRunTime_Air_1->setText(QString("%1 时 %2 分 %3 秒").arg(val/3600).arg((val%3600)/60).arg(val%60));
    else if(index == 1)
        ui->label_TotalRunTime_Air_2->setText(QString("%1 时 %2 分 %3 秒").arg(val/3600).arg((val%3600)/60).arg(val%60));
}

uint PageAccumlations::getTotalRunTimeInput()
{
    if(ui->lineEdit_TotalRunTime->text().isEmpty())
        return 0;
    return ui->lineEdit_TotalRunTime->text().toUInt();
}

uint PageAccumlations::getTotalRunDisInput()
{
    if(ui->lineEdit_TotalDistance->text().isEmpty())
        return 0;
    return ui->lineEdit_TotalDistance->text().toUInt();
}

uint PageAccumlations::getAirCompTimeInput(uint8_t index)
{
    if(index == 0)
    {
        if(ui->lineEdit_TotalRunTime_Air_1->text().isEmpty())
            return 0;
        return ui->lineEdit_TotalRunTime_Air_1->text().toUInt();
    }
    else if(index == 1)
    {
        if(ui->lineEdit_TotalRunTime_Air_6->text().isEmpty())
            return 0;
        return ui->lineEdit_TotalRunTime_Air_6->text().toUInt();
    }
    else
        return 0;
}

void PageAccumlations::on_Btn_TotalDistanceSet_clicked()
{
    emit signal_TotalDisSetValid(true);
    ui->Btn_TotalDistanceSet->setEnabled(false);
    QTimer::singleShot(2000,this,[=](){
        emit signal_TotalDisSetValid(false);
        ui->Btn_TotalDistanceSet->setEnabled(true);
    });
}

void PageAccumlations::on_Btn_TotalRunTimeSet_clicked()
{
    emit signal_TotalTimeSetValid(true);
    ui->Btn_TotalRunTimeSet->setEnabled(false);
    QTimer::singleShot(2000,this,[=](){
        emit signal_TotalTimeSetValid(false);
        ui->Btn_TotalRunTimeSet->setEnabled(true);
    });
}

void PageAccumlations::on_Btn_CompRunTImeSet1_clicked()
{
    emit signal_AirComp1TimeSetValid(true);
    ui->Btn_CompRunTImeSet1->setEnabled(false);
    QTimer::singleShot(2000,this,[=](){
        emit signal_AirComp1TimeSetValid(false);
        ui->Btn_CompRunTImeSet1->setEnabled(true);
    });
}

void PageAccumlations::on_Btn_CompRunTImeSet4_clicked()
{
    emit signal_AirComp6TimeSetValid(true);
    ui->Btn_CompRunTImeSet4->setEnabled(false);
    QTimer::singleShot(2000,this,[=](){
        emit signal_AirComp6TimeSetValid(false);
        ui->Btn_CompRunTImeSet4->setEnabled(true);
    });
}

uint PageAccumlations::getProEnergyInput()
{
    if(ui->lineEdit_ProEnergyConsume->text().isEmpty())
        return 0;
    return ui->lineEdit_ProEnergyConsume->text().toUInt();
}

uint PageAccumlations::getAuxEnergyInput()
{
    if(ui->lineEdit_AuxEnergyConsume->text().isEmpty())
        return 0;
    return ui->lineEdit_AuxEnergyConsume->text().toUInt();
}

uint PageAccumlations::getRegEnergyInput()
{
    if(ui->lineEdit_RegEnergyConsume->text().isEmpty())
        return 0;
    return ui->lineEdit_RegEnergyConsume->text().toUInt();
}

uint PageAccumlations::getBrkResEnergyInput()
{
    if(ui->lineEdit_BrakeResistEnergyConsume->text().isEmpty())
        return 0;
    return ui->lineEdit_BrakeResistEnergyConsume->text().toUInt();
}

void PageAccumlations::setTotalEnergyConsume(uint32_t energy)
{
    ui->label_TotalEnergyConsume->setText(QString("%1 kWh").arg(energy));
}

void PageAccumlations::setAuxEnergyConsume(uint32_t energy)
{
    ui->label_AuxEnergyConsume->setText(QString("%1 kWh").arg(energy));
}

void PageAccumlations::setHVACEnergyConsume(uint32_t energy)
{
    ui->label_HVACEnergyConsume->setText(QString("%1 kWh").arg(energy));
}

void PageAccumlations::setProEnergyConsume(uint32_t energy)
{
    ui->label_ProEnergyConsume->setText(QString("%1 kWh").arg(energy));
}

void PageAccumlations::setBrkResEnergyConsume(uint32_t energy)
{
    ui->label_BrkResEnergyConsume->setText(QString("%1 kWh").arg(energy));
}

void PageAccumlations::setRegEnergyConsume(uint32_t energy)
{
    ui->label_RegEnergyConsume->setText(QString("%1 kWh").arg(energy));
}

void PageAccumlations::on_Btn_ProEnergySet_clicked()
{
    emit signal_ProEnergySetValid(true);
    ui->Btn_ProEnergySet->setEnabled(false);
    QTimer::singleShot(2000,this,[=](){
        emit signal_ProEnergySetValid(false);
        ui->Btn_ProEnergySet->setEnabled(true);
    });
}

void PageAccumlations::on_Btn_AuxEnergySet_clicked()
{
    emit signal_AuxEnergySetValid(true);
    ui->Btn_AuxEnergySet->setEnabled(false);
    QTimer::singleShot(2000,this,[=](){
        emit signal_AuxEnergySetValid(false);
        ui->Btn_AuxEnergySet->setEnabled(true);
    });
}
